package com.tan.androidtest.anim.property;

import android.animation.AnimatorSet;
import android.animation.ArgbEvaluator;
import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.widget.TextView;

import com.tan.androidtest.R;
import com.tan.androidtest.base.BaseActivity;

/**
 * 测试属性动画
 */

public class TestViewPropertyAnimatorActivity extends BaseActivity {

    private TextView mTvTest;
    private TextView mTvTest1;
    private TextView mTvTest2;
    private MyProcessView mMyProcessView;

    @Override
    public int initLayoutId() {
        return R.layout.activity_view_property_animator_layout;
    }

    @Override
    public void initTitle(TextView tv_bar_left, TextView tv_bar_title, TextView tv_bar_right) {
        tv_bar_title.setText("测试属性动画");
    }

    @Override
    public void initView() {
        mTvTest = findViewById(R.id.tv_test);
        mTvTest1 = findViewById(R.id.tv_test_1);
        mTvTest2 = findViewById(R.id.tv_test_2);
        mMyProcessView = findViewById(R.id.my_process_view);

    }

    @Override
    public void initData() {
        //loadAnimByViewPropertyAnimator();
        //loadAnimByKeyframe();
        //loadAnimByAnimatorset();
        loadAnimByAnimatorset();
    }


    /**
     * 1.直接使用View的属性来做动画
     */
    public void loadAnimByViewPropertyAnimator() {
        mTvTest.animate().translationXBy(1000)
                .setDuration(1000);
        mTvTest1.animate().translationX(1000)
                .setDuration(1000);

        mTvTest2.animate().rotation(300).setDuration(2000);
    }

    /**
     * 使用AnimatorSet顺序执行多个动画
     */
    private void loadAnimByAnimatorset() {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mMyProcessView, "process", 0, 80);
        objectAnimator.setDuration(2000);

        ObjectAnimator objectAnimator1 = ObjectAnimator.ofInt(mMyProcessView, "color", 0xffff2e31, 0xff333333);
        objectAnimator1.setEvaluator(new ArgbEvaluator());
        objectAnimator1.setDuration(2000);
        objectAnimator1.start();

        AnimatorSet set = new AnimatorSet();
        set.playSequentially(objectAnimator, objectAnimator1);
        set.start();
    }


    /**
     * 使用PropertyValuesHolder同时执行对个动画
     */
    private void loadAnimByPropertyValuesHolder() {
        PropertyValuesHolder propertyValuesHolder1 = PropertyValuesHolder.ofFloat("alpha", 0, 1);
        PropertyValuesHolder propertyValuesHolder2 = PropertyValuesHolder.ofInt("color", 0xff000000, 0xffff2e31);
        propertyValuesHolder2.setEvaluator(new ArgbEvaluator());
        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(mMyProcessView, propertyValuesHolder1, propertyValuesHolder2);
        objectAnimator.setDuration(5000);
        objectAnimator.start();
    }

    /**
     * 使用Keyframe实现,把同一个属性拆分成多个阶段
     */
    private void loadAnimByKeyframe() {
        // 在 0% 处开始
        Keyframe keyframe1 = Keyframe.ofFloat(0, 0);
        // 时间经过 50% 的时候，动画完成度 100%
        Keyframe keyframe2 = Keyframe.ofFloat(0.8f, 90);
        // 时间见过 100% 的时候，动画完成度倒退到 80%，即反弹 20%
        Keyframe keyframe3 = Keyframe.ofFloat(1, 80);
        PropertyValuesHolder holder = PropertyValuesHolder.ofKeyframe("process", keyframe1, keyframe2, keyframe3);
        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(mMyProcessView, holder);
        objectAnimator.setDuration(2000);
        objectAnimator.start();
    }

}
